package cn.tedu.submarine;

import sun.security.x509.AVA;

import javax.swing.*;
import java.io.Serializable;
import java.util.Random;
import java.awt.Graphics;

public abstract class Property implements Serializable {

    public static final int LIVE=0;
    public static final int DEAD=1;
    protected int state=LIVE;//当前状态（默认为活着的）

    protected int x;
    protected int y;
    protected int width;
    protected int height;
    protected int speed;
    public Property(int width,int height){
        this.width=width;
        this.height=height;
        x=-width;
        Random random=new Random();
        y=random.nextInt(World.HEIGHT-height-150+1)+150;
        speed=random.nextInt(3)+1;
    }
    public Property(int width,int height,int x,int y,int speed){
        this.width=width;
        this.height=height;
        this.x=x;
        this.y=y;
        this.speed=speed;
    }
    //都要移动，但移动方式不一样 抽象
    public abstract void move();
    //都要获得图片，但图片不一样 抽象
    public abstract ImageIcon getImage();
    //判断是否是活着的
    public boolean isLive(){
        return state==LIVE;
    }
    //判断是否是死了的
    public boolean isDead(){
        return state==DEAD;
    }
    //画对象 g:画笔
    public void paintImage(Graphics g){
        if(this.isLive()) {
            this.getImage().paintIcon(null, g, this.x, this.y);
        }
    }
    //检测潜艇越界
    public boolean Delete(){
        return this.x>=World.WIDTH;
    }
    public boolean isHit(Property other){
        int x1=this.x-other.width;
        int y1=this.y-other.height;
        int x2=this.x+this.width;
        int y2=this.y+this.height;
        return other.x>=x1 && other.x<=x2 && other.y>=y1 && other.y<=y2;
    }
    public boolean isShipHit(Property other){
        return (other.x>=this.x&&other.x<=this.x+this.width&&other.y<=this.y+this.height);
    }
    public void goDead(){
        state=DEAD;
    }

}
